package com.itheima.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @Project: reggie_order_out
 * @Author: 29292
 * @Create: 2022-05-18 15:11
 * @Desc：
 **/
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionAdvice {

    @ExceptionHandler(SQLException.class)
    public R SQLException(SQLException sqlException){
        String sqlExceptionMessage = sqlException.getMessage();
        if (sqlExceptionMessage.contains("Duplicate entry")){
            return R.error(sqlExceptionMessage.split(" ")[2]+"-此用户名重复");
        }
        if (sqlExceptionMessage.contains("Data truncation")){
            return R.error(sqlExceptionMessage.split(" ")[7]+"-字段格式不正确");
        }
        return R.success("啥事没有!");
    }
}
